<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/30
 * Time: 15:53
 */
namespace app\team\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;
use app\team\model\Comment as App;
class Admin extends Common
{
    public function admin(){
        if (Request::instance()->isAjax()){
            $where['is_del'] = array('eq',0);
            $data = Db::name('team_admin')->alias('t1')->field('t1.*,t3.title as role_name,t3.id as role_id
')->join('team_auth_group_access t2','t1.t_admin_id = t2.uid','left')
                ->join('team_auth_group t3','t2.group_id = t3.id','left')
                ->where($where)->where('t1.team_id',Session::get('team')['team_id'])->order('t1.t_admin_id','desc')->select();
            return ['code'=>0,'msg'=>'','data'=>$data];
        }else{
            $role = Db::name('team_auth_group')->select();
            $this->assign('role',$role);
            return view();
        }
    }

    /**
     * 管理员添加
     */
    public function addAdmin(){
        if(Request::instance()->isAjax()){
            $post = Request::instance()->post();
            $post = $post['data'];
            //加密方式
            $hasher = new \PasswordHash(8, false);
            //将密码进行哈希，结果是一个长度为60个字符的字符串
            $post['password'] = $hasher->HashPassword($post['password']);
            //检查用户名是否存在
            $check = Db::name('team_admin')->where('t_admin_tel',$post['t_admin_tel'])->find();
            if ($check){
                return ['code'=>'该管理员已经存在'];
                exit;
            }
            if(empty($post['role_id'])){
                return ['code'=>'请先添加角色'];
                exit;
            }
            $role_id = $post['role_id'];

            unset($post['role_id']);

            $post['add_time'] = date('Y-m-d H:i:s');
            $post['team_id'] = Session::get('team')['team_id'];
            $post['pid'] = 1;
            $res = Db::name('team_admin')->insertGetId($post);
            if ($res){
                $group = array('uid'=>$res,'group_id'=>$role_id);
                Db::name('team_auth_group_access')->insert($group);
                return ['code'=>0];

            }else{
                return ['code'=>'添加失败'];
            }
        }
    }

    /**
     * 管理员修改
     */
    public function editAdmin(){
        if (Request::instance()->isAjax()){
            $post = Request::instance()->post();
            $saveData = array(
                'admin_rename'=>$post['data']['admin_rename'],
                't_admin_tel'=>$post['data']['t_admin_tel'],
            );


            $res = Db::name('team_admin')->where('t_admin_id',$post['data']['t_admin_id'])->update($saveData);
            $role = Db::name('team_auth_group_access')->where("uid",$post['data']['t_admin_id'])->setField('group_id',$post['data']['role_id']);
            if ($res!==false && $role!==false){

                return ['code'=>0];
            }else{
                return ['code'=>'修改失败'];
            }
        }
    }

    /**
     * 管理员密码修改
     */
    public function editAdminPass(){
        if (Request::instance()->isAjax()){
            $post = Request::instance()->post();
            $post = $post['data'];
            //加密方式
            $hasher = new \PasswordHash(8, false);
            //将密码进行哈希，结果是一个长度为60个字符的字符串
            $post['password'] = $hasher->HashPassword($post['password']);

            $res = Db::name('team_admin')->where('t_admin_id',$post['t_admin_id'])->setField('password',$post['password']);
            if ($res!==false){
                return ['code'=>0];
            }else{
                return ['code'=>1];
            }
        }
    }

    /**
     * 管理员删除
     */
    public function delAdmin(){
        if (Request::instance()->isAjax()){
            $post = Request::instance()->post();


            $res = Db::name('team_admin')->where('t_admin_id',$post['admin_id'])->delete();
            if ($res!==false){
                return ['code'=>0];
            }else{
                return ['code'=>1];
            }
        }
    }

    /**
     * 管理员禁用
     */
    public function editAdminIsUse(){
        if (Request::instance()->isAjax()){
            $post = Request::instance()->post();

            $data = [
                'is_use'=>$post['is_use']
            ];
            $res = Db::name('team_admin')->where('t_admin_id',$post['admin_id'])->update($data);
            if ($res!==false){
                return ['code'=>0];
            }else{
                return ['code'=>1];
            }
        }
    }
}